﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_OtherServicesManager : System.Web.UI.Page
{
    ServiceBlo _serviceBlo = new ServiceBlo();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDdlServiceType();
            LoadOtherService();            
            lblStatus.Visible = false;
        }
    }
    private void LoadDdlServiceType()
    {
        List<ServiceDto> _list = _serviceBlo.SelectOtherServiceType();
        ddlServices.DataSource = _list;
        ddlServices.DataTextField = "serviceName";
        ddlServices.DataValueField = "id_ST";
        ddlServices.DataBind();
    }
    protected void gdvOtherServices_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gdvOtherServices.EditIndex = e.NewEditIndex;
        LoadOtherService();
    }
    private void LoadOtherService()
    {
        string _serviceName=ddlServices.SelectedItem.ToString();        
        gdvOtherServices.DataSource = _serviceBlo.SelectService(_serviceName).ListServiceDetail;
        gdvOtherServices.DataBind();
    }
    protected void gdvOtherServices_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdvOtherServices.EditIndex = -1;
        LoadOtherService();
    }
    protected void gdvOtherServices_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int id_SD = int.Parse(gdvOtherServices.DataKeys[e.RowIndex].Value.ToString());
            int _duration = int.Parse(((TextBox)gdvOtherServices.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
            float _denomination = float.Parse(((TextBox)gdvOtherServices.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
            int _discount = int.Parse(((TextBox)gdvOtherServices.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
            string _package = ((TextBox)gdvOtherServices.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
            string _description = ((TextBox)gdvOtherServices.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            float _total;
            _total = _denomination * _duration * (100 - _discount) / 100;
            ServiceDetail _serviceDetail = new ServiceDetail(_package, _duration, 0, _description, _discount, _denomination, _total, id_SD);
            _serviceBlo.UpdateServiceDetail(_serviceDetail);
        }
        catch (Exception)
        {
            lblStatus.Visible = true;
            lblStatus.Text = " Your data is not valid! Please try again";
            gdvOtherServices.EditIndex = e.RowIndex;
            LoadOtherService();
            return;
        }
        gdvOtherServices.EditIndex = -1;
        LoadOtherService();
        lblStatus.Visible = true;
        lblStatus.Text = "Update successfull ";
    }
    protected void gdvOtherServices_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id_SD = int.Parse(gdvOtherServices.DataKeys[e.RowIndex].Value.ToString());
        if (((LinkButton)gdvOtherServices.Rows[e.RowIndex].Cells[5].FindControl("lbtnDeactive")).Visible == true)
        {
            _serviceBlo.UpdateServiceDetailStatus(id_SD, false);
            LoadOtherService();
        }
        else
        {
            _serviceBlo.UpdateServiceDetailStatus(id_SD, true);
            LoadOtherService();
        }
    }
    protected void ddlServices_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadOtherService(); 
    }
}